<?php

namespace app\controller;

use think\facade\Db;
use think\Request;

class User
{
    public function login(Request $request)
    {
        $account = ($request->param('account'));
        $password = ($request->param('password'));
        if (empty($account) or empty($password)) {
            return json()->data(['code' => 400, 'msg' => '账号密码不能为空']);
        } else {
            $res = Db::name('user')->where("account='$account'")->where("password='$password'")->select();
            if (count($res) === 1) {
                return json()->data(['data' => $res[0], 'code' => 200, 'msg' => '成功']);
            } else {
                return json()->data(['code' => 400, 'msg' => '账号密码输入错误']);
            }
        }
    }
    public function logout(Request $request)
    {
        $id = $request->header('authorization');
        if ($id) {
            Db::name('user')->where("id='$id'")->select();
            return json()->data(['code' => 200, 'msg' => '成功']);
        } else {
            return json()->data(['code' => 2001, 'msg' => '未登录']);
        }
    }
    public function change_pwd(Request $request)
    {
        $token = $request->header('authorization');
        if ($token) {
            if (Db::name('user')->where("token='$token'")->update(['password' => $request->param('pwd')])) {
                return json()->data(['code' => 200, 'msg' => '成功']);
            } else {
                return json()->data(['code' => 2001, 'msg' => '未登录']);
            }
        } else {
            return json()->data(['code' => 2001, 'msg' => '未登录']);
        }
    }
}
